package lysjmaster.base.common.spring.jpa;

import lombok.AllArgsConstructor;
import lombok.Getter;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.util.Collection;
import java.util.function.BiFunction;

/**
 * @author Created by zk on 2019/12/7 13:24
 * @Description jpa条件构造枚举
 */
@Getter
@AllArgsConstructor
@SuppressWarnings("unchecked")
public enum PredicateEnum {
    //equal
    EQ((predicateDTO, criteriaBuilder) -> criteriaBuilder.equal(predicateDTO.getPath(), predicateDTO.getVal())),
    //not equal
    NE((predicateDTO, criteriaBuilder) -> criteriaBuilder.notEqual(predicateDTO.getPath(), predicateDTO.getVal())),
    //grater than
    GT((predicateDTO, criteriaBuilder) -> criteriaBuilder.greaterThan(predicateDTO.getPath(), (Comparable) predicateDTO.getVal())),
    //grater than equal
    GE((predicateDTO, criteriaBuilder) -> criteriaBuilder.greaterThanOrEqualTo(predicateDTO.getPath(), (Comparable) predicateDTO.getVal())),
    //less than
    LT((predicateDTO, criteriaBuilder) -> criteriaBuilder.lessThan(predicateDTO.getPath(), (Comparable) predicateDTO.getVal())),
    //less than equal
    LE((predicateDTO, criteriaBuilder) -> criteriaBuilder.lessThanOrEqualTo(predicateDTO.getPath(), (Comparable) predicateDTO.getVal())),
    //full like %XX%
    F_LK((predicateDTO, criteriaBuilder) -> criteriaBuilder.like(predicateDTO.getPath(), "%"+predicateDTO.getVal()+"%")),
    //left like %XX
    L_LK((predicateDTO, criteriaBuilder) -> criteriaBuilder.like(predicateDTO.getPath(), "%"+predicateDTO.getVal())),
    //right like XX%
    R_LK((predicateDTO, criteriaBuilder) -> criteriaBuilder.like(predicateDTO.getPath(), predicateDTO.getVal()+"%")),
    //in
    IN((predicateDTO, criteriaBuilder) -> predicateDTO.getPath().in((Collection) predicateDTO.getVal())),
    //not in
    NI((predicateDTO, criteriaBuilder) -> predicateDTO.getPath().in((Collection) predicateDTO.getVal()).not())
    ;
    private BiFunction<PredicateDTO,CriteriaBuilder,Predicate> predicateFunc;
}
